package com.simulate2.demo.service;

import com.simulate2.demo.dao.CommisionRepository;
import com.simulate2.demo.dao.OrderDao;
import com.simulate2.demo.dao.OrdersByDayDao;
import com.simulate2.demo.dao.UserRepository;
import com.simulate2.demo.entity.*;
import com.simulate2.demo.util.MongoUtil;
import com.simulate2.demo.util.MySubUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;

@Service
public class OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private OrdersByDayDao ordersByDayDao;
    @Autowired
    private CommisionRepository commisionRepository;
    @Autowired
    MongoUtil mongoUtil;

    /**
     * 按照日期与数量生成该日期的订单
     *
     * @param n
     * @param date
     */
    public void produceOrder(int n, int date) {

        List<Orders> ordersList = new ArrayList<>();
        Random rand = new Random();
        for (int i = 0; i < n; i++) {

            //int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
            //生成100-10000的价格
            double price = rand.nextInt(9901) + 100 + Math.random();
            //生成2-5%的佣金比例
            double ratio = rand.nextInt(4) + 2 + Math.random();
            //截取，保留后两位小数
            DecimalFormat df = new DecimalFormat("#.00");
            BigDecimal bd = new BigDecimal(price);
            price = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            BigDecimal bd2 = new BigDecimal(ratio);
            ratio = bd2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            if (ratio > 5) ratio = 5.0;

            Orders orders = new Orders(0, price, "goods", ratio, date);
            ordersList.add(orders);

        }
        orderDao.saveAll(ordersList);
        orderDao.flush();
    }

    /**
     * 根据日期，查询数据库中所有该日期的数据，累加其分拥金额
     * 查询所有用户的已激活总积分，累加总积分，存入数据库
     *
     * @param date
     */
    public void caluOrdersByDay(int date) {

        //分拥金额
        BigDecimal commisionSum = orderDao.getAllCommision(date);

        BigDecimal allCredit = userRepository.getSum();

        OrdersByDay o = new OrdersByDay(date, allCredit, commisionSum);
        ordersByDayDao.save(o);
        Date date1 = new Date();                    //获得当前时间
        System.out.println("获取创建1000 0000用户的List所需时间---->开始时间："+date1);
        List<User> userList = new ArrayList<>(10000100);
        userList = userRepository.getAll();
        Date date2 = new Date();                    //获得当前时间
        System.out.println("结束时间："+date2+"  共耗时："+(date2.getTime()-date1.getTime()));

        Date date3 = new Date();                    //获得当前时间
        System.out.println("将10万数据导入mongoDB所需时间---->开始时间："+date3);
        List<List<User>> ls= MySubUtil.subList(userList,10000);
        BigDecimal commisionPerCredit=commisionSum.divide(allCredit,10,BigDecimal.ROUND_HALF_DOWN);//计算每积分的分拥
        /*Date date5 = new Date();                    //获得当前时间
        System.out.println("将10万数据分拆为10个1万数据的list---->结束时间："+date5+"  共耗时："+(date5.getTime()-date3.getTime()));*/

        /*for(List<User> l:ls){
            System.out.println("新列表开始一个");
            mongoUtil.caluCommision(date, allCredit, commisionSum,l);
        }*/
        /*List<CompletableFuture> l=new ArrayList<>();
        for(int i=0;i<ls.size();i++){
            CompletableFuture<String> page1 =mongoUtil.caluCommision(date, allCredit, commisionSum,ls.get(i));
        }*/

        CompletableFuture<String> page1 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(0));
        CompletableFuture<String> page2 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(1));
        CompletableFuture<String> page3 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(2));
        CompletableFuture<String> page4 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(3));
        CompletableFuture<String> page5 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(4));
        CompletableFuture<String> page6 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(5));
        CompletableFuture<String> page7 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(6));
        CompletableFuture<String> page8 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(7));
        CompletableFuture<String> page9 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(8));
        CompletableFuture<String> page10 =mongoUtil.caluCommision(date, commisionPerCredit,ls.get(9));
        CompletableFuture.allOf(page1,page2,page3,page4,page5,page6,page7,page8,page9,page10).join();

        /*CompletableFuture<String> page1=mongoUtil.caluCommision(date, commisionPerCredit,userList);
        CompletableFuture.allOf(page1).join();*/


        Date date4 = new Date();                    //获得当前时间
        System.out.println("将10万数据导入mongoDB所需时间---->结束时间："+date4+"  共耗时："+(date4.getTime()-date3.getTime()));



    }


}
